package com.xhsj.user.lsm.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.xhsj.user.event.event.MessageEvent;
import com.xhsj.user.hr.system.mapper.TbGlobalUserMapper;
import com.xhsj.user.hr.process.hrLeaveProcess.mapper.TbHrApprovalLeaveApplyMapper;
import com.xhsj.user.hr.system.service.TbGlobalUserService;
import com.xhsj.user.lsm.entity.*;
import com.xhsj.user.lsm.mapper.*;
import com.xhsj.user.lsm.model.MyContractApply;
import com.xhsj.user.sms.base.BaseService;
import com.xhsj.user.sms.process.entity.TbPurchaseProcessHiTask;
import com.xhsj.user.sms.process.entity.TbPurchaseProcessNode;
import com.xhsj.user.sms.process.entity.TbPurchaseProcessTask;
import com.xhsj.user.sms.process.mapper.TbPurchaseProcessHiTaskMapper;
import com.xhsj.user.sms.process.mapper.TbPurchaseProcessNodeMapper;
import com.xhsj.user.sms.process.mapper.TbPurchaseProcessTaskMapper;
import com.xhsj.user.utils.IdGen;
import com.xhsj.user.utils.MessageConstant;
import com.xhsj.user.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 合同申请表 服务实现类
 * </p>
 *
 * @author suxiaolin
 * @since 2020-05-11
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class TbContractApplyService extends BaseService<TbContractApplyMapper, TbContractApply>{
    @Autowired
    private TbContractApplyMapper dao;

    @Autowired
    private TbBadgesProcessTaskService tbBadgesProcessTaskService;

    @Autowired
    private TbBadgesProcessOpinionMapper tbBadgesProcessOpinionMapper;

    @Autowired
    private TbContractOpinionFileMapper tbContractOpinionFileMapper;

    @Autowired
    private TbBadgesProcessOpinionService tbBadgesProcessOpinionService;

    @Autowired
    private TbContractFileService tbContractFileService;

    @Autowired
    private TbContractBadgesManagerService tbContractBadgesManagerService;

    @Autowired
    private TbContractBadgesManagerMapper tbContractBadgesManagerMapper;
    @Autowired
    private TbBadgesUseApplyService tbBadgesUseApplyService;
    @Autowired
    private TbPurchaseProcessHiTaskMapper tbPurchaseProcessHiTaskMapper;
    @Autowired
    private TbPurchaseProcessTaskMapper tbPurchaseProcessTaskMapper;
    @Autowired
    private TbPurchaseProcessNodeMapper tbPurchaseProcessNodeMapper;
    @Autowired
    private TbGlobalUserService tbGlobalUserService;

    @Autowired
    private ApplicationContext applicationContext;

    //角色名称 财务
    @Value("${rolekeyname.teller}")
    private String teller;


    @Override
    //@CacheEvict(cacheNames="TbContractApply", allEntries=true)
    public boolean save(TbContractApply entity) {
        return super.save(entity);
    }

    @Override
    //@Cacheable(key = "#id",sync = true)
    public TbContractApply getById(Serializable id) {
        TbContractApply byId = super.getById(id);
        // 查询合同文件列表
        QueryWrapper<TbContractFile> wrapper = new QueryWrapper<>();
        wrapper.eq("contract_apply_id",id);
//        List<TbContractFile> list = tbContractFileService.list(wrapper);
        List<TbContractFile> list = tbContractFileService.contractFileListByDataId(id);
        byId.setContractFile(list);
        // 查询证章列表
        QueryWrapper<TbContractBadgesManager> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("contract_apply_id",id);
        wrapper1.select("manager_id");
        List<TbContractBadgesManager> list1 = tbContractBadgesManagerService.list(wrapper1);
        List<String> collect = list1.stream().map(TbContractBadgesManager::getManagerId).collect(Collectors.toList());
        byId.setManagerIds(collect);
        return byId;
    }

    @Override
    //@CacheEvict(cacheNames="TbContractApply", allEntries=true)
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    //@CacheEvict(cacheNames="SysDict", allEntries=true)
    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }

    //@Cacheable(key = "#data",sync = true)
    public IPage<MyContractApply> selectPage(Map<String,Object> data) {
        int currentPage=1;
        if (!Objects.isNull(data.get("page"))) {
            currentPage=(Integer) data.get("page");
        }
        int pageSize=20;
        if (!Objects.isNull(data.get("size"))) {
            pageSize=(Integer) data.get("size");
        }

//        TbContractApply entity = JSON.parseObject(JSON.toJSONString(data), TbContractApply.class);
//        Page<TbContractApply> page = new Page(currentPage,pageSize);
//        QueryWrapper<TbContractApply> wrapper = new QueryWrapper(entity);
//        wrapper.eq("create_by",entity.getCreateBy());
//        entity.setCreateBy(null);

        String partiesName = StringUtils.getStringValue(data, "partiesName");
        String contractNo = StringUtils.getStringValue(data, "contractNo");

        Page<MyContractApply> page = new Page(currentPage,pageSize);
        IPage<MyContractApply> listPage = dao.myContractApply(page,String.valueOf(data.get("createBy")),contractNo,partiesName);
//        super.selectPage(page, wrapper)
        return listPage;
    }

    public IPage<TbContractApply> findListByUserId(Map<String, Object> data) {
        int currentPage=1;
        if (!Objects.isNull(data.get("page"))) {
            currentPage=(Integer) data.get("page");
        }
        int pageSize=20;
        if (!Objects.isNull(data.get("size"))) {
            pageSize=(Integer) data.get("size");
        }

        Object userId = data.get("userId");


        TbContractApply entity = JSON.parseObject(JSON.toJSONString(data), TbContractApply.class);
        Page<TbContractApply> page = new Page(currentPage,pageSize);
        QueryWrapper<TbContractApply> wrapper = new QueryWrapper(entity);
        wrapper.eq("create_by",userId);
        return super.selectPage(page, wrapper);
    }

    @Override
    //@Cacheable(value = "TbContractApply",sync = true)
    public List<TbContractApply> selectAll() {
        return super.selectAll();
    }


    //合同公共流程
    public boolean contractExecProcess(Map<String, Object> data) throws Exception {
        try {
            //保存意见和意见附件
//            execProcessOpinion(data);
//            data.replace("opinionText","");
            Integer submitStatus = StringUtils.toInteger(data.get("submitStatus"));
            data.put("type",2);
            //修改流程节点名称和提交状态
            TbContractApply tbContractApply = new TbContractApply();
            if(submitStatus==2) {
                String s = tbBadgesProcessTaskService.handleAudit(data);
                tbContractApply.setNodeName(s);
            }
            // 数据id
            String dataId = String.valueOf(data.get("dataId"));
            tbContractApply.setId(dataId);
            tbContractApply.setSubmitStatus(submitStatus);
            boolean b = this.updateById(tbContractApply);
            if (!b) {
                log.error("修改合同申请节点名或提交状态称失败");
                throw new Exception("修改合同申请节点名或提交状态称失败");
            }

            sendMessage(data, dataId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e);
        }
        return true;
    }

    private void sendMessage(Map<String, Object> data, String dataId) {

        String fullName = dao.getFullNameByDataId(dataId);
        // 流程申请用户id
        String proposerId = String.valueOf(data.get("proposerId"));
        // 状态
        Integer status = StringUtils.toInteger(data.get("status"));// 对应节点表

        // 登录用户id
        String createBy = String.valueOf(data.get("createBy"));
        // 下一个节点审核用户id
        Object userId =  data.get("userId");
        // 下一个节点审核用户角色
        Object roleIds = data.get("roleIds");

        // 节点id
        String nodeId = String.valueOf(data.get("nodeId"));

        List<Long> userIds = Lists.newArrayList();
        if (!Objects.isNull(userId)) {
            List<String> userIdList = JSONArray.parseArray(String.valueOf(userId), String.class);
            for (String u : userIdList) {
                userIds.add(Long.valueOf(u));
            }
        }
        if (!Objects.isNull(roleIds)) {
            List<String> role_Ids = JSONArray.parseArray(String.valueOf(roleIds), String.class);
            List<Long> collectRoleIds = role_Ids.stream().map(Long::parseLong).collect(Collectors.toList());
            List<Long> userListByRoleIds = tbGlobalUserService.getUserListByRoleIds(collectRoleIds);
            userIds.addAll(userListByRoleIds);
        }

        // 消息发送
        Map<String, Object> messageMap = Maps.newHashMap();
        messageMap.put("projectId",2);
        messageMap.put("dataId",dataId);
        messageMap.put("title", MessageConstant.MSG_13);
        messageMap.put("name", fullName);
        messageMap.put("sender",proposerId);
        messageMap.put("examineStatus",1);
        messageMap.put("userIds",userIds);


        if (status == 1) { // 审核退回
            userIds.add(Long.valueOf(proposerId));
            messageMap.put("title", MessageConstant.MSG_13);
            messageMap.put("examineStatus",2);
            messageMap.put("sender",createBy);
            messageMap.put("userIds",userIds);

        }

        applicationContext.publishEvent(new MessageEvent(this,messageMap));
    }

    //审批意见及附件
    public Boolean execProcessOpinion(Map<String,Object> data) throws Exception {
        // 审批意见 先删除后增加
//        TbBadgesProcessOpinion processOpinion =
//                StringUtils.toJavaBean(
//                        data.get("contractOpinion"), TbBadgesProcessOpinion.class);

        TbBadgesProcessOpinion processOpinion = new TbBadgesProcessOpinion();
        processOpinion.setOpinionText(String.valueOf(data.get("opinionText")));
        if (StringUtils.isNotBlank(processOpinion)) {
            if (StringUtils.isNotBlank(processOpinion.getId())) {
                //删除意见
                if (!tbBadgesProcessOpinionService.removeById(String.valueOf(processOpinion.getId()))) {
                    log.error("删除意见数据失败");
                    throw new Exception("删除意见数据失败");
                }
                //删除意见相关的附件
                QueryWrapper wrapper = new QueryWrapper();
                wrapper.eq("opinion_id", processOpinion.getId());
                if (!tbBadgesProcessOpinionService.remove(wrapper)) {
                    log.error("删除意见附件数据失败");
                    throw new Exception("删除意见附件数据失败");
                }
            }
            Date date = new Date();
            processOpinion.setId(IdGen.uuid());
            processOpinion.setDataId(String.valueOf(data.get("dataId")));
            processOpinion.setNodeId(String.valueOf(data.get("nodeId")));
            processOpinion.setCreateBy(String.valueOf(data.get("createBy")));
            processOpinion.setUpdateBy(String.valueOf(data.get("createBy")));
            processOpinion.setCreateDate(date);
            processOpinion.setUpdateDate(date);
            int insert = tbBadgesProcessOpinionMapper.insert(processOpinion);
            if (insert != 1) {
                log.error("意见数据失败");
                throw new Exception("保存意见数据失败");
            }

            //意见附件 先删除后增加
            List<Map<String,Object>> tbPurchaseProcessOpinionFileList = (List<Map<String, Object>>) data.get("contractOpinionFile");
            if (tbPurchaseProcessOpinionFileList != null) {
                for (Map<String,Object> processOpinionFile : tbPurchaseProcessOpinionFileList
                ) {
                    TbContractOpinionFile opinionFile= StringUtils.toJavaBean(processOpinionFile,TbContractOpinionFile.class);
                    opinionFile.setId(IdGen.uuid());
                    opinionFile.setOpinionId(processOpinion.getId());
                    opinionFile.setCreateBy(String.valueOf(data.get("createBy")));
                    opinionFile.setUpdateBy(String.valueOf(data.get("createBy")));
                    opinionFile.setUpdateDate(date);
                    int insert1 = tbContractOpinionFileMapper.insert(opinionFile);
                    if (insert1 != 1) {
                        log.error("保存意见附件数据失败");
                        throw new Exception("保存意见附件数据失败");
                    }
                }
            }
        }
        return true;
    }

    public boolean saveContractApply(TbContractApply entity) throws Exception {
        boolean save = super.save(entity);
        if (save) {
            List<TbContractFile> contractFile = entity.getContractFile();
            for (TbContractFile tbContractFile : contractFile) {
                if (StringUtils.isBlank(tbContractFile.getId())) {
                    tbContractFile.setId(IdGen.uuid());
                }
                tbContractFile.setContractApplyId(entity.getId());
            }

            if (contractFile.size()>0) {
                boolean b = tbContractFileService.saveOrUpdateBatch(contractFile);
                if (!b) {
                    log.error("保存合同文件出错");
                    throw new Exception("保存合同文件出错");
                }
            }

            List<TbContractBadgesManager> objects = Lists.newArrayList();
            List<String> managerIds = entity.getManagerIds();
            for (String managerId : managerIds) {
                TbContractBadgesManager badgesManager = new TbContractBadgesManager();
                badgesManager.setId(IdGen.uuid());
                badgesManager.setContractApplyId(entity.getId());
                badgesManager.setManagerId(managerId);
                objects.add(badgesManager);
            }


            QueryWrapper<TbContractBadgesManager> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("contract_apply_id",entity.getId());
            tbContractBadgesManagerMapper.delete(queryWrapper);

            if (objects.size()>0) {
                boolean b1 = tbContractBadgesManagerService.saveBatch(objects);
                if (!b1) {
                    log.error("保存合同-证章出错");
                    throw new Exception("保存合同-证章出错");
                }
            }


            return true;
        }else{
            return false;
        }
    }

    public TbContractApply startProcess(TbContractApply data) throws Exception {
        try {

            boolean save = this.saveContractApply(data);
            if (!save){
                log.error("合同数据保存出错");
                throw new Exception("合同数据保存出错");
            }
            // 查询领导
            String leader = String.valueOf(tbGlobalUserService.getLeaderId(StringUtils.toLong(data.getCreateBy())));
//            String leader = tbGlobalUserService.getLeaderByUserId(StringUtils.toLong(data.getCreateBy()));
            if (StringUtils.isBlank(leader) || leader == "null") {
                throw new Exception("部门领导不存在,请联系管理员添加相关人员");
            }

            Map<String, Object> map = StringUtils.objectToMap(data);
            map.put("dataId",data.getId());
            map.put("type",2);
            map.put("userId",leader);
            String s = tbBadgesProcessTaskService.startProcess(map);
            data.setNodeName(s);
            boolean b = this.updateById(data);
            if (!b){
                throw new Exception("修改节点名称失败");
            }
//

//            // 如果是采购合同,修改供应链相关数据
//            if (data.getContractType() == 1 && StringUtils.isNotBlank(data.getPurchaseOrderId())) {
//                this.updatePurchaseOrder(data);
//            }
            if(StringUtils.isNotBlank(map.get("userId"))) {
                map.put("userId", Arrays.asList(String.valueOf(map.get("userId")).split(",")));
            }
            sendMessage(map, data.getId());
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            throw e;
        }
        return data;
    }


    //合同流程-综合管理部审核提交
    public boolean contractIntegratedManagementAudit(Map<String, Object> data) throws Exception {
        //流程相关的
        this.contractExecProcess(data);
        //保存合同文件
        return saveContractFile(data);
    }

    //保存合同文件
    public boolean saveContractFile(Map<String, Object> data) throws Exception {
        Object contractFile1 = data.get("contractFile");
        if (!Objects.isNull(contractFile1)) {
            List<TbContractFile> contractFileList = JSON.parseArray(JSON.toJSONString(contractFile1), TbContractFile.class);
            if (contractFileList.size()>0) {
                for (TbContractFile contractFile:contractFileList) {
                    if (StringUtils.isBlank(contractFile.getId())) {
                        contractFile.setId(IdGen.uuid());
                    }
                    contractFile.setContractApplyId(String.valueOf(data.get("dataId")));
                    contractFile.setCreateBy(String.valueOf(data.get("createBy")));
                    contractFile.setUpdateBy(String.valueOf(data.get("createBy")));
                }
                if(!tbContractFileService.saveOrUpdateBatch(contractFileList)){
                    throw new Exception("保存合同文件失败");
                }
            }
        }

        return true;
    }

    //保存用章记录
    public boolean saveUseBadgesRecord(Map<String, Object> data) throws Exception {
        //获取合同申请相关的数据
        String dataId = String.valueOf(data.get("dataId"));
        //获取当前合同申请的数据
        Map<String, Object> contractApplyList = dao.getContractApplyList(dataId);
        String contractSubject = String.valueOf(contractApplyList.get("contractSubject"));
        String managerIds = String.valueOf(contractApplyList.get("managerIds"));
        String badgesNames = String.valueOf(contractApplyList.get("badgesNames"));
        TbBadgesUseApply entity=new TbBadgesUseApply();
        entity.setAcType(0);
//        entity.setSignificanceIdentify(0);
        entity.setApplyReason(contractSubject);
        entity.setBadgesManagerId(managerIds);
        entity.setNodeName("使用中");
        entity.setUseSource(2);
        entity.setUseWay(0);
        entity.setName(badgesNames);
        entity.setCreateBy(String.valueOf(data.get("proposerId")));
        entity.setUpdateBy(String.valueOf(data.get("createBy")));
        if(!tbBadgesUseApplyService.save(entity)) {
                log.error("保存用章记录出错");
                throw new Exception("保存用章记录出错");
            }
        return true;
    }


    //合同流程-审批记录
    public IPage<Map> auditRecordList(Map<String,Object> data) {

        int currentPage=1;
        if (!Objects.isNull(data.get("page"))) {
            currentPage=(Integer) data.get("page");
        }
        int pageSize=20;
        if (!Objects.isNull(data.get("size"))) {
            pageSize=(Integer) data.get("size");
        }
        String dataId = String.valueOf(data.get("dataId"));

        Page<Map> page = new Page(currentPage,pageSize);
        return  dao.auditRecordList(page,dataId);
    }

    //合同流程-重新提交
    public boolean returnCommitAudit(Map<String, Object> data) throws Exception {
        TbContractApply tbContractApply = StringUtils.toJavaBean(data, TbContractApply.class);
        tbContractApply.setId(String.valueOf(data.get("dataId")));
        if (!this.saveContractApply(tbContractApply)) {
            log.error("合同数据保存出错");
            throw new Exception("合同数据保存出错");
        }

        if (StringUtils.isNotBlank(tbContractApply.getSubtaskId())) {
            int i = dao.updateOrderSubTask(tbContractApply.getSubtaskId(),tbContractApply.getPaymentWay(),tbContractApply.getAdvanceProportion(),tbContractApply.getSecondProportion(),tbContractApply.getFinalProportion());
            if (i==0) {
                throw new Exception("修改采购订单节点名称出错");
            }
        }
        return this.contractExecProcess(data);
    }

    //合同流程-总经理审核提交
    public boolean contractGeneralManagerAudit(Map<String, Object> data) throws Exception {
        //流程相关的
        this.contractExecProcess(data);
        //保存合同使用证章记录
        return saveUseBadgesRecord(data);
    }



    public void contractLegalReviewAudit(Map<String, Object> data) throws Exception {
        this.contractExecProcess(data);
        Object purchaseNodeId = data.get("purchaseNodeId");
        Object contractType = data.get("contractType");
        if (StringUtils.toInteger(contractType) == 1 && StringUtils.isNotBlank(purchaseNodeId)) {
            this.handlePassPurchaseContract(data);
        }

    }



    // 处理采购合同通过 修改采购流程
    public void handlePassPurchaseContract(Map<String, Object> data) throws Exception {
        Object purchaseNodeId = data.get("purchaseNodeId");// 节点名称

        Object subtaskId = data.get("subtaskId");// 采购子任务id

        Object paymentWay = data.get("paymentWay");// 采购子任务id
        int status = 0;
        if (StringUtils.isNotBlank(paymentWay)) {
            String s = String.valueOf(paymentWay);
            if ("1".equals(s)) {// 表示一次结清
                status = 11;// 直接提交到财务
            }

        }

        Map<String, Object> map = tbPurchaseProcessNodeMapper.getNextNodeId(String.valueOf(purchaseNodeId), status, "2");
        Object nextNodeId = map.get("next_node_id");

//        // 下下个节点
//        Map<String, Object> map1 = tbPurchaseProcessNodeMapper.getNextNodeId(String.valueOf(nextNodeId), 0, "2");
//        Object nextNodeId1 = map1.get("next_node_id");

        String id = String.valueOf(map.get("nextId"));
        TbPurchaseProcessNode nextNode = tbPurchaseProcessNodeMapper.selectById(id);
        String nodeName = nextNode.getNodeName();


        updatePurchaseTask(data, String.valueOf(subtaskId), purchaseNodeId, nextNodeId,nodeName,status);


    }


    private void updatePurchaseTask(Map<String, Object> data, String subtaskId, Object nextNodeId, Object nextNodeId1,String nodeName,int status) throws Exception {
        QueryWrapper<TbPurchaseProcessTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("data_id",subtaskId);
        queryWrapper.eq("node_id",nextNodeId);
        // 查询正在执行中任务
        TbPurchaseProcessTask tbPurchaseProcessTask = tbPurchaseProcessTaskMapper.selectOne(queryWrapper);
        if (tbPurchaseProcessTask !=null) {
            TbPurchaseProcessHiTask tbPurchaseProcessHiTask = new TbPurchaseProcessHiTask();
            tbPurchaseProcessHiTask.setId(tbPurchaseProcessTask.getId());
            tbPurchaseProcessHiTask.setStatus(0);
            tbPurchaseProcessHiTask.setUpdateBy(String.valueOf(data.get("createBy")));
            tbPurchaseProcessHiTask.setUpdateDate(new Date());
            // 修改历史任务
            int insert = tbPurchaseProcessHiTaskMapper.updateById(tbPurchaseProcessHiTask);
            if (insert == 0) {
                throw new Exception("保存历史任务出错");
            }

            // 删除正在执行任务
            int i1 = tbPurchaseProcessTaskMapper.deleteTask(tbPurchaseProcessTask.getDataId(), tbPurchaseProcessTask.getNodeId());
            if (i1 == 0) {
                throw new Exception("删除任务出错");
            }

            if (status == 11) {
                // 查询出纳
                List<String> roleListByRoleName = tbGlobalUserService.getRoleListByRoleName(teller);
                for (String roleId : roleListByRoleName) {
                    // 添加下一个节点执行任务
                    TbPurchaseProcessTask purchaseProcessTask =
                            new TbPurchaseProcessTask(
                                    "", roleId, "", String.valueOf(subtaskId), String.valueOf(nextNodeId1), String.valueOf(data.get("createBy")), String.valueOf(data.get("proposerId")),"2");
                    purchaseProcessTask.setCreateDate(new Date());
                    purchaseProcessTask.setId(IdGen.uuid());

                    int insert1 = tbPurchaseProcessTaskMapper.insert(purchaseProcessTask);
                    if (insert1 == 0) {
                        log.error("保存数据失败");
                        throw new Exception("保存数据失败");
                    }

                    TbPurchaseProcessHiTask hiTask = new TbPurchaseProcessHiTask();
                    BeanUtils.copyProperties(purchaseProcessTask,hiTask);
                    hiTask.setStatus(2);
                    hiTask.setUserId("");
                    hiTask.setUpdateDate(null);
                    hiTask.setUpdateBy("");
                    int insert2 = tbPurchaseProcessHiTaskMapper.insert(hiTask);

                    if (insert2 == 0) {
                        log.error("保存数据失败");
                        throw new Exception("保存数据失败");
                    }

                }

            }else{
                // 添加下一个节点执行任务
                TbPurchaseProcessTask purchaseProcessTask =
                        new TbPurchaseProcessTask(
                                String.valueOf(data.get("proposerId")), "", "", String.valueOf(subtaskId), String.valueOf(nextNodeId1), String.valueOf(data.get("createBy")), String.valueOf(data.get("proposerId")),"2");
                purchaseProcessTask.setCreateDate(new Date());
                purchaseProcessTask.setId(IdGen.uuid());

                int insert1 = tbPurchaseProcessTaskMapper.insert(purchaseProcessTask);
                if (insert1 == 0) {
                    log.error("保存数据失败");
                    throw new Exception("保存数据失败");
                }

                TbPurchaseProcessHiTask hiTask = new TbPurchaseProcessHiTask();
                BeanUtils.copyProperties(purchaseProcessTask,hiTask);
                hiTask.setStatus(2);
                hiTask.setUserId("");
                hiTask.setUpdateDate(null);
                hiTask.setUpdateBy("");
                int insert2 = tbPurchaseProcessHiTaskMapper.insert(hiTask);
                if (insert2 == 0) {
                    log.error("保存数据失败");
                    throw new Exception("保存数据失败");
                }
            }

            // TODO: 2020/5/29 这里添加消息发送
            // 修改节点名称
            dao.updateOrderSubTaskName(nodeName,String.valueOf(subtaskId));
        }
    }

    public IPage<Object> findContractApplyList(IPage<TbContractApply> page, String userId) {
        return dao.findContractApplyList(page,userId);
    }
}
